package ${package.Controller};

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${package.Entity}.${entity};
import com.its.common.model.BaseJson;
import ${package.Service}.${table.serviceName};
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* ${table.comment!}前端控制器
* @author ${author}
* @since ${date}
*/
@Controller
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
@Slf4j
public class ${table.controllerName} {
    @Autowired
    private ${table.serviceName} ${table.serviceName?uncap_first};

    /**
    * 条件查询
    */
    @GetMapping(value = "allData")
    @ResponseBody
    public BaseJson<List<${table.entityName}>> allData(@RequestParam(required = false) String id) {
        ${table.entityName} query = new ${table.entityName}();
        if(StringUtils.isNotBlank(id)){
            query.setId(id);
        }
        Wrapper<${table.entityName}> queryWrapper = new QueryWrapper<${table.entityName}>(query);
        List<${table.entityName}> list = ${table.serviceName?uncap_first}.list(queryWrapper);
        return BaseJson.data(list);
    }

    /**
    * 条件分页查询
    */
    @GetMapping(value = "pageData")
    @ResponseBody
    public BaseJson<Page<${table.entityName}>> pageData(@RequestParam int page, @RequestParam int limit,
        @RequestParam(required = false) String orderBy, @RequestParam(required = false) Boolean isAsc){
        ${table.entityName} query = new ${table.entityName}();
        QueryWrapper<${table.entityName}> queryWrapper = new QueryWrapper<${table.entityName}>(query);
        if(StringUtils.isNotBlank(orderBy) && null != isAsc){
            if(isAsc){
                queryWrapper.orderByAsc(orderBy);
            }else{
                queryWrapper.orderByDesc(orderBy);
            }
        }
        IPage<${table.entityName}> pageQuery = new Page<${table.entityName}>(page, limit);
        Page<${table.entityName}> pageInfo = (Page<${table.entityName}>) ${table.serviceName?uncap_first}.page(pageQuery,queryWrapper);
        return BaseJson.data(pageInfo);
    }

    /**
    * 根据ID查询
    */
    @GetMapping(value = "detail/{id}")
    @ResponseBody
    public BaseJson<${table.entityName}> detail(@PathVariable String id) {
        ${table.entityName} result = null;
        if(StringUtils.isNotBlank(id)){
            ${table.entityName} query = new ${table.entityName}();
            query.setId(id);
            result = ${table.serviceName?uncap_first}.getById(id);
        }
        return BaseJson.data(result);
    }

    /**
    * 保存（新增或根据ID更新）
    */
    @PostMapping(value = "save")
    @ResponseBody
    public BaseJson<String> save(@RequestBody ${table.entityName} entity) {
        if(null == entity){
            return BaseJson.fail("保存对象不能为空");
        }
        boolean result = ${table.serviceName?uncap_first}.saveOrUpdate(entity);
        return (result == true) ? BaseJson.success("保存成功") : BaseJson.fail("保存错误");
    }

    /**
    * 逻辑删除 将del_flag设置为1
    * @param id
    * @return
    */
    @PostMapping(value = "delete/{id}")
    @ResponseBody
    public BaseJson<String> delete(@PathVariable String id) {
        ${table.entityName} ${table.entityName?uncap_first} = ${table.serviceName?uncap_first}.getById(id);
        if(null == ${table.entityName?uncap_first}){
            return BaseJson.success("逻辑删除成功");
        }
        ${table.entityName?uncap_first}.setDelFlag("1");
        boolean result = ${table.serviceName?uncap_first}.updateById(${table.entityName?uncap_first});
        return (result == true) ? BaseJson.success("逻辑删除成功") : BaseJson.fail("逻辑删除失败");
    }

    /**
    * 物理删除
    * @param id
    * @return
    */
    @PostMapping(value = "purge/{id}")
    @ResponseBody
    public BaseJson<String> purge(@PathVariable String id) {
        if(StringUtils.isBlank(id)){
            return BaseJson.fail("ID不能为空");
        }
        boolean result = ${table.serviceName?uncap_first}.removeById(id);
        return (result == true) ? BaseJson.success("物理删除成功") : BaseJson.fail("物理删除失败");
    }
}
